<?php
// +----------------------------------------------------------------------
// | zhanshop-marketing / VisitDevice.php    [ 2024/12/18 15:28 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2024 zhangqiquan All rights reserved.
// +----------------------------------------------------------------------
// | Author: zhangqiquan <768617998@qq.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\model;

use zhanshop\App;
use zhanshop\database\Model;

class VisitDevice extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'visit_device';

    // 设置当前模型的数据库连接
    protected $connection = 'mysql';
    public function search($deviceInfo)
    {
        $whererRaw = '';
        if($deviceInfo['oaid']){
            $whererRaw .= 'oaid ="'.addslashes(strval($deviceInfo['oaid'])).'" or ';
        }
        if($deviceInfo['imei']){
            $whererRaw .= 'imei ="'.addslashes(strval($deviceInfo['imei'])).'" or ';
        }
        if($deviceInfo['android_id']){
            $whererRaw .= 'android_id ="'.addslashes(strval($deviceInfo['android_id'])).'" or ';
        }
        if($deviceInfo['idfa']){
            $whererRaw .= 'idfa ="'.addslashes(strval($deviceInfo['idfa'])).'" or ';
        }
        if($deviceInfo['mac']){
            $whererRaw .= 'mac ="'.addslashes(strval($deviceInfo['mac'])).'" or ';
        }

        $whererRaw = rtrim($whererRaw, 'or ');

        if($whererRaw == false){
            App::error()->setError("oaid、imei、android_id、idfa、mac不能都为空", 406);
        }
        $device = $this->where(['app' => $deviceInfo['app']])->whereRaw($whererRaw)->find();
        if($device){
            return $device;
        }
        return [];
    }

    /**
     * 注册设备
     * @param $deviceInfo
     * @return int
     */
    public function register($deviceInfo)
    {
        return $this->insertGetId($deviceInfo);
    }
}